package com.baizhi.service.impl;

import com.baizhi.dao.CategoryDao;
import com.baizhi.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    @Override
    public List<Category> selectCate() {
        SqlSession session = MyBatisUtils.openSession();
        List<Category> cate=new ArrayList<>();
        try {
            CategoryDao mapper = session.getMapper(CategoryDao.class);
            cate = mapper.select();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MyBatisUtils.close(session);
        }
        return cate;
    }

    @Override
    public Category findMessage(int book_id) {
        SqlSession session = MyBatisUtils.openSession();
        Category cate = new Category();
        try {
            CategoryDao mapper = session.getMapper(CategoryDao.class);
            cate = mapper.findMessage(book_id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MyBatisUtils.close(session);
        }
        return cate;
    }

    @Override
    public List<Category> selectCateAndCount(int id) {
        SqlSession session = MyBatisUtils.openSession();
        List<Category> cate = new ArrayList<>();
        try {
            CategoryDao mapper = session.getMapper(CategoryDao.class);
            cate = mapper.selectCateAndCount(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MyBatisUtils.close(session);
        }
        return cate;
    }

    @Override
    public List<Category> selectCateAndCountSecond(int id) {
        SqlSession session = MyBatisUtils.openSession();
        List<Category> cate = new ArrayList<>();
        try {
            CategoryDao mapper = session.getMapper(CategoryDao.class);
            cate = mapper.selectCateAndCountSecond(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MyBatisUtils.close(session);
        }
        return cate;
    }

    @Override
    public Category selectCate(int id) {
        SqlSession session = MyBatisUtils.openSession();
        Category cate = new Category();
        try {
            CategoryDao mapper = session.getMapper(CategoryDao.class);
            cate = mapper.selectCate(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MyBatisUtils.close(session);
        }
        return cate;
    }
}
